package org.jeecg.modules.bus.controller;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.annotation.ApiLogin;
import org.jeecg.com.util.ThirdSessionHolder;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.TenantContextHolder;
import org.jeecg.config.ApiVersion;
import org.jeecg.config.ApiVersionConstant;
import org.jeecg.config.WebConfig;
import org.jeecg.modules.business.entity.BusBookingRooms;
import org.jeecg.modules.business.entity.BusRoomsLivingOrder;
import org.jeecg.modules.business.service.IBusBookingRoomsService;
import org.jeecg.modules.business.service.IBusHotelService;
import org.jeecg.modules.business.service.IBusRoomBookingOrdersService;
import org.jeecg.modules.business.service.IBusRoomsLivingOrderService;
import org.jeecg.modules.order.entity.CesOrderMessage;
import org.jeecg.modules.order.service.ICesOrderMessageService;
import org.jeecg.modules.rooms.entity.CesRooms;
import org.jeecg.modules.rooms.service.CesRoomsServiceImpl;
import org.jeecg.modules.mall.entity.MallUserInfo;
import org.jeecg.modules.wxuser.service.IUserInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description: ces_order_message
 * @Author: jeecg-boot
 * @Date:   2023-03-24
 * @Version: V1.0
 */
@Api(tags="留言投诉添加")
@RestController
@RequestMapping("/mall-api/hotel-order-message")
@Slf4j
public class OrderMessageController extends WebConfig {
	@Resource
	private ICesOrderMessageService cesOrderMessageService;
	 @Resource
	 private IBusHotelService busHotelService;
	@Resource
	private IUserInfoService userInfoService;
	@Resource
	private IBusRoomsLivingOrderService busRoomsLivingOrderService;
	@Resource
	private IBusRoomBookingOrdersService busRoomBookingOrdersService;
	@Resource
	private IBusBookingRoomsService busBookingRoomsService;
	@Resource
	private CesRoomsServiceImpl cesRoomsService;


	/**
	 *   留言/投诉添加
	 *
	 * @param cesOrderMessage
	 * @return
	 */
	@ApiOperation(value="留言/投诉添加", notes="留言/投诉添加")
	@ApiLogin
	@ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CesOrderMessage cesOrderMessage) {
		String tenantId = TenantContextHolder.getTenantId();
		String userId = ThirdSessionHolder.getUserId();
		cesOrderMessage.setTenantId(tenantId);
		cesOrderMessage.setUserId(userId);
		cesOrderMessage.setCreateDate(DateTime.now());
		MallUserInfo mallUserInfo = userInfoService.getById(userId);
		if (mallUserInfo != null) {
			cesOrderMessage.setUserMobile(mallUserInfo.getMobile());
			cesOrderMessage.setUserName(mallUserInfo.getNickName());
		}
		LambdaQueryWrapper<BusRoomsLivingOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(BusRoomsLivingOrder::getThirdLoginUserId, userId).last("limit 1");
		BusRoomsLivingOrder busRoomsLivingOrder = busRoomsLivingOrderService.getOne(lambdaQueryWrapper);
		if (busRoomsLivingOrder != null && StringUtils.isNotBlank(busRoomsLivingOrder.getBookingOrderId())) {
			LambdaQueryWrapper<BusBookingRooms> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
			lambdaQueryWrapper1.eq(BusBookingRooms::getBookingOrdersId, busRoomsLivingOrder.getBookingOrderId()).last("limit 1");
			BusBookingRooms bookingRooms = busBookingRoomsService.getOne(lambdaQueryWrapper1);
			if (bookingRooms != null) {
				CesRooms cesRooms = cesRoomsService.getById(bookingRooms.getRoomId());
				if (cesRooms != null) {
					cesOrderMessage.setRoomNo(cesRooms.getName());
				}
			}
		}
		cesOrderMessageService.save(cesOrderMessage);
		return Result.OK("添加成功！");
	}


}
